Meeting 3 & 4 Notes
Meeting 3: Introduction to Python¶
Resources¶
- Repl.it
- Python Documentation
- Python Library Reference (for standard modules)
- Example Hangman Game made in Python
Notes¶
Follow along with the steps below to create your own Hangman/word guessing game in Python! Each step builds on the code from the previous step.
Note that we will be using Python 2 for this lesson.
Example: https://hangman.redapple410.repl.run
Example code: https://repl.it/@redapple410/hangman
0: Getting Started¶
Explanation:
Go to repl.it and create a new repl (project). Choose Python 2 as the language.
1: Output¶
Explanation:
Imagine that you are playing hangman, and need to pick a word. Output this word using print "The correct word is ..."
.
Example:
2: Variables¶
Explanation:
Take your word from Step 1, and create a variable (perhaps call it word
?) to store it using <name> = <value>
. Then, output its value using print "The correct word is %s." % (<name>)
.
Example:
3: Input¶
Explanation:
Create a new variable (perhaps call it guess
?) and use <name> = raw_input("Guess the word: ")
to get input from the user. It's also a good idea to output the user's guess.
Example:
4: If/Else Statements¶
Explanation:
After getting input, check to see if the user's guess matches the correct word using if guess == word:
. If it matches, output something nice! Otherwise, use else:
to tell them that the guess is incorrect.
Example:
5: While Loops¶
Explanation:
Use a while
loop to let the user keep guessing the word until they get it right (ie. keep guessing while guess != word
). You can even implement another variable to keep count of how many guesses it takes!
Example:
6: For Loops¶
Explanation:
Now, instead of asking the user to guess the entire word, ask them to guess just one letter instead. Every time they guess a letter, use a for
loop to go through every letter of the correct word and compare it to the user's guess. If they match, output something (eg. print "Found letter %s at index %d" % (guess, i)
).
Example:
7: String Operations¶
Explanation:
Use string concatenation and string slicing like word[:i] + guess + word[i+1:]
to keep track of which letters the user has correctly guess so far, and output them to the screen.
Example:
8: Lists¶
Explanation:
Create a pre-defined list of words (perhaps call it words
?) that you can choose from.
Example:
9: Importing Modules¶
Explanation:
Import the random
module using import random
. Then, use word = random.choice(words)
to randomly choose a word from the list you created in Step 8, and use it as the correct word for Hangman.
(For those who are too lazy to consult the Library Reference, random.choice(<list>)
returns a random item from the given list.)
Example: